<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
    <van-form @submit="onSubmit">
      <van-field
      required
        v-model="formList.mobile"
        type="tel"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请输入正确的手机号',pattern :/^1[3-9]\d{9}$/ ,trigger:'onChange'} ]"
      />
      <van-field
      required
        v-model="formList.code"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码',pattern :/^\d{6}$/,trigger:'onChange' }]"
      />
      <div style="margin: 16px">
        <van-button color="#48b784"
        round
        block type="info"
        :loading="isLoading"
        :disabled="isLoading"
        native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      formList: {
        mobile: '13888888888',
        code: '246810'
      },
      isLoading: false
    }
  },
  methods: {
    ...mapActions(['asyncLoginApi']),
    async onSubmit (values) {
      // 发起请求的时候禁用按钮
      this.isLoading = true
      // 请求的过程
      try {
        await this.asyncLoginApi(this.formList)
        Notify({ type: 'success', message: '登录成功' })
        this.$router.replace('/layout')
      } catch (error) {
        console.log(error)
        Notify('登陆失败')
      }
      // 请求完成之后不管成功与否 都可以再次发起请求
      this.isLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
// .van-nav-bar{
//   background-color: #48b784;
// }
// /deep/ .van-nav-bar__title{
//   color: #fff;
// }
</style>
